package test.serverframe.armc.server.manager.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import test.serverframe.armc.server.manager.controller.vo.InterfaceParamHistoryVo;
import test.serverframe.armc.server.manager.controller.vo.TaskHistoryVo;
import test.serverframe.armc.server.manager.dao.mapper.InterfaceInvokingRecordMapper;
import test.serverframe.armc.server.manager.dao.mapper.InvokeTaskRecordMapper;
import test.serverframe.armc.server.manager.domain.InterfaceInvokingRecord;
import test.serverframe.armc.server.manager.domain.params.HistoryInterfaceParams;
import test.serverframe.armc.server.manager.domain.params.HistoryRecordParam;

/**  
* 
* @author yegui  
* @date 2018年12月12日  
*/
@Service
public class InterfaceInvokingRecordService {
	
	@Autowired
	private InterfaceInvokingRecordMapper interfaceRecordMapper;
	
	@Autowired
	private  InvokeTaskRecordMapper invokeTaskRecordMapper;
	
	public void insert(InterfaceInvokingRecord record) {
		interfaceRecordMapper.insert(record);
	}

	/**
	 * @author yegui  
	 * @date 2018年12月19日  
	 * @param interfaceRecord
	 * void
	 */  
	public void insertSelective(InterfaceInvokingRecord interfaceRecord) {
		interfaceRecordMapper.insertSelective(interfaceRecord);
	}
	
	/**
	 * 查询历史接口返回值、状态、断言结果和接口参数记录
	 * @author yegui  
	 * @date 2019年1月21日  
	 * @param HistoryInterfaceParams
	 * 
	 */ 
	public TaskHistoryVo findInterfaceParamsHistory(HistoryInterfaceParams params) {
		HistoryRecordParam record = new HistoryRecordParam();
		record.setInvokeRecordId(params.getInvokeRecordId());
		record.setTaskId(params.getTaskId());
		//查询任务记录下的接口图
		TaskHistoryVo taskHistoryVo = new TaskHistoryVo();
		List<TaskHistoryVo> vos = invokeTaskRecordMapper.getTaskRecord(record);
		for (TaskHistoryVo vo : vos) {
			taskHistoryVo.setNodeArr(vo.getNodeArr());
			taskHistoryVo.setTaskRecordId(vo.getTaskRecordId());
			break;
		}
		params.setTaskRecordId(taskHistoryVo.getTaskRecordId());
		List<InterfaceParamHistoryVo> histroys = new ArrayList<>();
		TaskHistoryVo taskVo  = new TaskHistoryVo();
		for (String nodeId : params.getNodeIds()) {
			params.setNodeId(nodeId);
			InterfaceParamHistoryVo interVo = interfaceRecordMapper.findInterfaceParamsHistory(params);
			if (interVo != null)
				interVo.setNodeId(nodeId);
			histroys.add(interVo);
		}
		taskVo.setNodeArr(taskHistoryVo.getNodeArr());
		taskVo.setHistroys(histroys);
		return taskVo;
		
	}

	/**
	 * @author yegui  
	 * @date 2018年12月19日  
	 * @param interfaceRecordFinish
	 * void
	 */  
	public void updateByPrimaryKeySelective(InterfaceInvokingRecord interfaceRecordFinish) {
		interfaceRecordMapper.updateByPrimaryKeySelective(interfaceRecordFinish);
		
	}
}
